package com.clw.controller;

import com.clw.dataobject.Buyer;
import com.clw.enums.ResultEnum;
import com.clw.exception.SellException;
import com.clw.form.BuyerForm;
import com.clw.service.BuyerService;
import com.clw.utils.ResultVOUtils;
import com.clw.utils.StringUtils;
import com.clw.vo.ResultVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 卖家用户
 * 2017-07-30 15:28
 */
@Controller
@RequestMapping("/buyer")
public class BuyerController {

    @Autowired
    private BuyerService buyerService;

    @RequestMapping("/list")
    public ModelAndView list(){
        return new ModelAndView("buyer/index");
    }
    @ResponseBody
    @RequestMapping("/getData")
    public Map<String,Object> getData(@RequestParam(value = "page", defaultValue = "1") Integer page,
                                      @RequestParam(value = "rows", defaultValue = "10") Integer rows,
                                      HttpServletRequest request) {
        PageRequest request1=new PageRequest(page-1,rows);
        String name=request.getParameter("name");
        String phone=request.getParameter("phone");
        Page<Buyer> sellerInfos=buyerService.findAll(name,phone,request1);
        Map<String,Object> map=new HashMap<>();
        map.put("rows",sellerInfos.getContent());
        map.put("total",buyerService.getSize(name,phone));
        return map;
    }

    @RequestMapping("/view")
    public ModelAndView addView(@RequestParam(value = "id",required = false) Integer id,
                                Map<String,Object> map){
        if (StringUtils.isNotEmpty(id)){
            Buyer buyer=buyerService.findOne(id);
            map.put("buyer",buyer);
        }
        return new ModelAndView("buyer/add_view");
    }


    @ResponseBody
    @PostMapping("del")
    public ResultVO del(@RequestParam(value = "id",required = false) Integer id){
        Buyer buyer=buyerService.findOne(id);
        if (buyer==null){
            return ResultVOUtils.error(ResultEnum.NO_FOUND.getCode(),
                    ResultEnum.NO_FOUND.getMessage());
        }
        buyerService.del(buyer);
        return ResultVOUtils.success("删除成功");
    }

    @RequestMapping("/save")
    @ResponseBody
    public ResultVO save(@Valid BuyerForm form, BindingResult bindingResult){
        if (bindingResult.hasErrors()){
            return ResultVOUtils.error(ResultEnum.PARAM_ERROR.getCode(),bindingResult.getFieldError().getDefaultMessage());
        }
        Buyer buyer=new Buyer();
        try{
            if (StringUtils.isNotEmpty(form.getId())){
                buyer=buyerService.findOne(form.getId());
            }
            BeanUtils.copyProperties(form,buyer);
            buyerService.save(buyer);
        }catch (SellException e){
            return ResultVOUtils.error(ResultEnum.SAVE_ERROR.getCode(),
                    ResultEnum.SAVE_ERROR.getMessage());
        }
        return ResultVOUtils.success("保存成功");
    }

    @ResponseBody
    @RequestMapping("/getAllData")
    public List<Buyer> getAllData(){
        return buyerService.findAll();
    }
}
